Grokking Amazon Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Amazon Easy Problems
Redistribute Characters to Make All Strings Equal (easy)
Solution: Redistribute Characters to Make All Strings Equal
Longest Common Prefix (easy)
Solution: Longest Common Prefix
Make Array Zero by Subtracting Equal Amounts (easy)
Solution: Make Array Zero by Subtracting Equal Amounts
Leaf-Similar Trees (easy)
Solution: Leaf-Similar Trees
Path Crossing (easy)
Solution: Path Crossing
Special Array With X Elements Greater Than or Equal X (easy)
Solution: Special Array With X Elements Greater Than or Equal X
Relative Sort Array (easy)
Solution: Relative Sort Array
Transpose Matrix (easy)
Solution: Transpose Matrix
Find Common Characters (easy)
Solution: Find Common Characters
Fizz Buzz (easy)
Solution: Fizz Buzz
Increasing Order Search Tree (easy)
Solution: Increasing Order Search Tree
Largest Odd Number in String (easy)
Solution: Largest Odd Number in String
Best Time to Buy and Sell (easy)
Solution: Best Time to Buy and Sell Stock
Take Gifts From the Richest Pile(easy)
Solution: Take Gifts From the Richest Pile
Remove Duplicates from Sorted List (easy)
Solution: Remove Duplicates from Sorted List
Maximum Length of Subarray With Positive Product (medium)
Solution: Maximum Length of Subarray With Positive Product
Largest Submatrix With Rearrangements (medium)
Solution: Largest Submatrix With Rearrangements
Count the Number of Good Subarrays (medium)
Solution: Count the Number of Good Subarrays
Minimum Suffix Flips (medium)
Solution: Minimum Suffix Flips
Minimum Number of Operations to Make Array Empty (medium)
Solution: Minimum Number of Operations to Make Array Empty
Next Greater Element II (medium)
Solution: Next Greater Element II
Make Sum Divisible by P (medium)
Solution: Make Sum Divisible by P
Rotting Oranges (medium)
Solution: Rotting Oranges
Minimum Deletions to Make Character Frequencies Unique (medium)
Solution: Minimum Deletions to Make Character Frequencies Unique
Minimum Adjacent Swaps to Make a Valid Array (medium)
Solution: Minimum Adjacent Swaps to Make a Valid Array
Reverse Integer (medium)
Solution: Reverse Integer
Maximize Distance to Closest Person (medium)
Solution: Maximize Distance to Closest Person
Split Linked List in Parts (medium)
Solution: Split Linked List in Parts
Maximum Difference Between Node and Ancestor (medium)
Solution: Maximum Difference Between Node and Ancestor
Deepest Leaves Sum (medium)
Solution: Deepest Leaves Sum
The kth Factor of n (medium)
Solution: The kth Factor of n
Coin Change II (medium)
Solution: Coin Change II
Sum of Subarray Minimums (medium)
Solution: Sum of Subarray Minimums
Valid Sudoku (medium)
Solution: Valid Sudoku
Longest Increasing Subsequence (medium)
Solution: Longest Increasing Subsequence
Next Permutation (medium)
Solution: Next Permutation
Subsets (medium)
Solution: Subsets
Combination Sum (medium)
Solution: Combination Sum
Number of Provinces (medium)
Solution: Number of Provinces
Number of Islands (medium)
Solution: Number of Islands
Constrained Subsequence Sum (hard)
Solution: Constrained Subsequence Sum
Number of Valid Subarrays (hard)
Solution: Number of Valid Subarrays
Count Vowels Permutation (hard)
Solution: Count Vowels Permutation
Concatenated Words (hard)
Solution: Concatenated Words
Minimum Replacements to Sort the Array (hard)
Solution: Minimum Replacements to Sort the Array
Count All Valid Pickup and Delivery Options (hard)
Solution: Count All Valid Pickup and Delivery Options
Length of the Longest Valid Substring (hard)
Solution: Length of the Longest Valid Substring
Count Array Pairs Divisible by K (hard)
Solution: Count Array Pairs Divisible by K
Word Ladder (hard)
Solution: Word Ladder
Path with Maximum Sum (hard)
Solution: Path with Maximum Sum
Solution: Transpose Matrix

Problem Statement

Given a 2D array matrix, return the transpose of the matrix.

In the transpose matrix, the first row of the original matrix becomes the first column, the second row becomes the second column, and so on.

Examples

  • Example 1:
    • Input:
[[1,2],
 [3,4]]
  • Expected Output:
[[1,3],
 [2,4]]
  • Justification: The first row [1,2] becomes the first column [1,3], and the second row [3,4] becomes the second column [2,4].

  • Example 2:

    • Input:
[[5,6,7],
 [8,9,10],
 [11,12,13]]
  • Expected Output:
[[5,8,11],
 [6,9,12],
 [7,10,13]]
  • Justification: The matrix is rearranged such that each row becomes a column in the transposed matrix.

  • Example 3:

    • Input:
[[14,15,16,17],
 [18,19,20,21],
 [22,23,24,25],
 [26,27,28,29]]
  • Expected Output:
[[14,18,22,26],
 [15,19,23,27],
 [16,20,24,28],
 [17,21,25,29]]
  • Justification: Similar to the previous examples, each row of the original matrix is transformed into a column in the transposed matrix.

Solution

To solve this problem, we'll create a new matrix where the number of rows equals the number of columns in the original matrix, and vice versa. The key idea is to iterate through each element of the original matrix and assign it to the appropriate position in the new matrix. This approach ensures that all elements are moved to their correct positions in the transposed matrix.

We believe this method is effective because it directly addresses the definition of matrix transposition by rearranging elements based on their indices. It's efficient as it requires only a single pass through the original matrix, making it both straightforward and suitable for large matrices.

Step-by-step Algorithm

  1. Initialize a new matrix for the transposed result. The dimensions of this new matrix will be the transpose of the original matrix's dimensions (i.e., if the original matrix has dimensions n x m, the transposed matrix will have dimensions m x n).

  2. Iterate through the rows of the original matrix. Use a loop that iterates from 0 to n - 1, where n is the number of rows in the original matrix.

  3. Iterate through the columns of the original matrix within each row iteration. Use a nested loop that iterates from 0 to m - 1, where m is the number of columns in the original matrix.

  4. Assign each element to its new position in the transposed matrix. During the nested iteration, for each element at position (i, j) in the original matrix, assign its value to position (j, i) in the new transposed matrix.

  5. Return the transposed matrix as the output of the function.

Algorithm Walkthrough

Let's consider the input: matrix = [[5,6,7],[8,9,10],[11,12,13]]

Let's walk through the algorithm step by step using the provided example matrix.

  1. Initialize the transposed matrix: Since the input matrix has dimensions 3 x 3 (3 rows and 3 columns), we initialize a new matrix with the same dimensions 3 x 3, as the input matrix is square. This new matrix starts as [[0,0,0],[0,0,0],[0,0,0]].

  2. Iterate through the rows of the original matrix:

    • First iteration (i = 0): The focus is on the first row [5,6,7].

      • Nested iteration for columns:
        • j = 0: The first element (5) is placed in the transposed matrix at (0, 0). The transposed matrix now is [[5,0,0],[0,0,0],[0,0,0]].
        • j = 1: The second element (6) is placed in the transposed matrix at (1, 0). The transposed matrix now is [[5,0,0],[6,0,0],[0,0,0]].
        • j = 2: The third element (7) is placed in the transposed matrix at (2, 0). The transposed matrix now is [[5,0,0],[6,0,0],[7,0,0]].
    • Second iteration (i = 1): The focus shifts to the second row [8,9,10].

      • Nested iteration for columns:
        • j = 0: The first element (8) is placed in the transposed matrix at (0, 1). The transposed matrix now is [[5,8,0],[6,0,0],[7,0,0]].
        • j = 1: The second element (9) is placed in the transposed matrix at (1, 1). The transposed matrix now is [[5,8,0],[6,9,0],[7,0,0]].
        • j = 2: The third element (10) is placed in the transposed matrix at (2, 1). The transposed matrix now is [[5,8,0],[6,9,0],[7,10,0]].
    • Third iteration (i = 2): Now looking at the third row [11,12,13].

      • Nested iteration for columns:
        • j = 0: The first element (11) is placed in the transposed matrix at (0, 2). The transposed matrix now is [[5,8,11],[6,9,0],[7,10,0]].
        • j = 1: The second element (12) is placed in the transposed matrix at (1, 2). The transposed matrix now is [[5,8,11],[6,9,12],[7,10,0]].
        • j = 2: The third element (13) is placed in the transposed matrix at (2, 2). The transposed matrix is now complete as [[5,8,11],[6,9,12],[7,10,13]].
  3. Return the transposed matrix: The final step is to return the fully transposed matrix, which in this case is [[5,8,11],[6,9,12],[7,10,13]].

Code

Java
Saved to Cloud

. . . .

Complexity Analysis

Time Complexity

The time complexity is , where n is the number of rows and m is the number of columns in the input matrix. This is because each element in the n x m matrix is visited exactly once to place it in its new position in the transposed matrix.

Space Complexity

The space complexity is as well, due to the creation of a new transposed matrix of size n x m. No additional space besides the output matrix is used, so the space complexity directly corresponds to the size of the output.

Transpose Matrix (easy)
Find Common Characters (easy)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-step Algorithm

Algorithm Walkthrough

Code

Complexity Analysis

Time Complexity

Space Complexity